package com.zhou.org.controller;

import com.zhou.framework.aop.anotation.LogOption;
import com.zhou.framework.aop.enums.LogLevel;
import com.zhou.framework.dao.Log;
import com.zhou.framework.model.RS;
import com.zhou.framework.util.PageUtil;
import com.zhou.org.dto.LogQueryDto;
import com.zhou.org.service.LogService;
import com.zhou.util.DateUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @author lang.zhou
 * @since 2023/2/19
 */
@Slf4j
@RestController
@RequestMapping("/sys/log")
public class LogController {

    @Autowired
    private LogService logService;

    @ApiOperation("日志列表")
    @LogOption(LogLevel.QUERY)
    @PostMapping("/list")
    public Object list(@RequestBody LogQueryDto dto){
        PageUtil.offsetPage(dto);
        List<Log> list = logService.listInfo(dto);
        return PageUtil.result(dto,list);
    }

    @ApiOperation("id查询日志")
    @LogOption(LogLevel.QUERY)
    @GetMapping("/get/{id}")
    public Object get(@PathVariable Integer id){
        return RS.data(logService.getById(id));
    }

    @GetMapping("/getSevenCount")
    @ApiOperation("查询近7日访问统计")
    @LogOption(LogLevel.QUERY)
    public Object getSevenCount(){
        Date now = new Date();
        TreeMap<String, Long> list = logService.getSevenCount();
        List<String> x = new ArrayList<>(list.keySet());
        List<Long> y = new ArrayList<>(list.size());
        Map<String,Long> t = new HashMap<>(list.size());

        for(int i =1; i<= 7;i++){
            String d = DateUtil.getLastDay(now,i);
            d = d.substring(0,4) + "-" + d.substring(4,6) + "-" +d.substring(6,8);
            if(!list.containsKey(d)){
                t.put(d,0L);
                x.add(d);
            }else{
                t.put(d,list.get(d));
            }
        }
        x.sort(Comparator.comparing(o -> Integer.valueOf(o.replace("-", ""))));
        for (String s : x) {
            y.add(t.get(s));
        }
        Map<String,Object> res = new HashMap<>(2);
        res.put("x",x);
        res.put("y",y);
        return RS.data(res);
    }

    @GetMapping("/getAccessStatusCount")
    @ApiOperation("请求健康状态统计图")
    @LogOption(LogLevel.QUERY)
    public Object getAccessStatusCount(HttpServletRequest request){

        Map<String,Long> list = logService.getAccessStatusCount();
        Map<String,Object> res = new HashMap<>(2);
        List<Map<String,Object>> data = new ArrayList<>(list.size());
        List<String> column = new ArrayList<>(list.size());
        Map<String,Boolean> select = new HashMap<>(list.size());
        long okC = 0;
        long tc = 0;
        for (String status : list.keySet()) {
            long cnt = list.get(status);
            tc += cnt;
            if("Success".equals(status)){
                okC = cnt;
            }
            column.add(status);
            select.put(status,true);
            Map<String,Object> m = new HashMap<>(2);
            m.put("name",status);
            m.put("value",cnt);
            data.add(m);
        }
        if(column.size() >= 3 && okC > tc* 0.6){
            select.put("ok",false);
        }
        res.put("column",column);
        res.put("data",data);
        res.put("select",select);
        return RS.data(res);
    }
}
